package leetcode.editor.cn;

public class _124_BinaryTreeMaximumPathSum {
    public static void main(String[] args) {
        Solution solution = new _124_BinaryTreeMaximumPathSum().new Solution();
    }
    //leetcode submit region begin(Prohibit modification and deletion)

    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    class Solution {
        int res = Integer.MIN_VALUE;

        private int oneSide(TreeNode root) {
            if (root == null) {
                return 0;
            }
            int l = Math.max(0, oneSide(root.left));
            int r = Math.max(0, oneSide(root.right));
            int curPath = l + r + root.val;
            res = Math.max(res, curPath);
            return root.val + Math.max(l, r);
        }

        public int maxPathSum(TreeNode root) {
            oneSide(root);
            return res;
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}